﻿#include <QStandardItemModel>
#include <QJsonObject>
#include <QAction>
#include <QMessageBox>
#include <src/GUI/uidelegates/uitableitemdelegate.h>
#include "uibucketstablewidget.h"
#include "ui_uibucketstablewidget.h"
#include "uicreatebucketdialog.h"
#include "src/middle/config/common.h"

UiBucketsTableWidget::UiBucketsTableWidget(QWidget *parent)
    : QWidget(parent), ui(new Ui::UiBucketsTableWidget)
{
    ui->setupUi(this);

    // (MVC) 为表格视图设置数据模型
    ui->tableView->setModel(MG->mModels->modelBuckets());

    // 设置列宽度(最后一列占满整个表)
    ui->tableView->setColumnWidth(0, 200);
    ui->tableView->setColumnWidth(1, 200);
    ui->tableView->horizontalHeader()->setStretchLastSection(true);

    // 隐藏表格的表头
    ui->tableView->verticalHeader()->setHidden(true);

    // 启动表格的列排序功能
    ui->tableView->setSortingEnabled(true);

    // 设置翻页按钮, 支持每页 10/50/100 条记录
    ui->widgetPage->setMaxRowList(QList<int>() << 10 << 50 << 100);
    connect(ui->widgetPage, &UiPageWidget::pageNumChanged, this,
            &UiBucketsTableWidget::onPageNumChanged);

    // 设置获取存储桶成功时的行为
    connect(MG->mSignal, &ManSignals::bucketsSuccess, this,
            &UiBucketsTableWidget::onBucketsSuccess);

    // 设置列表视图可以通过右键删除桶
    ui->tableView->setContextMenuPolicy( // 允许在表格上点击右键时显示操作菜单
            Qt::ActionsContextMenu);
    QAction *delAction = new QAction(QString::fromLocal8Bit("删除"), this);
    connect(delAction, &QAction::triggered, this,
            &UiBucketsTableWidget::onDelBucket);
    ui->tableView->addAction(delAction);

    // 为表格视图设置一个自定义的委托(代理)
    // 注: 这里目的是让鼠标悬停在表格的 item 上时,
    //     高亮一整行而不是默认高亮一个 item
    ui->tableView->setItemDelegate(new UiTableItemDelegate(ui->tableView));

    ui->btnCreateBucket->setProperty("style_button", "main");
}

UiBucketsTableWidget::~UiBucketsTableWidget()
{
    delete ui;
}

void UiBucketsTableWidget::on_tableView_doubleClicked(const QModelIndex &index)
{
    // 获取 objects 列表
    if (index.column() == 0) {
        QJsonObject params;
        params["bucketName"] = index.data().toString();
        params["dir"] = "";
        MG->mGate->send(API::OBJECTS::LIST, params);
    }
}

void UiBucketsTableWidget::onBucketsSuccess(const QList<MyBucket> &buckets)
{
    ui->widgetPage->setTotalRow(buckets.size());

    QStandardItemModel *model = MG->mModels->modelBuckets();
    for (int i = 0; i < model->rowCount(); ++i) {
        ui->tableView->setRowHeight(i, 40);
    }
}

void UiBucketsTableWidget::onPageNumChanged(int start, int maxLen)
{
    QStandardItemModel *model = MG->mModels->modelBuckets();

    for (int i = 0; i < model->rowCount(); ++i) {
        bool hidden = (i < start || i >= (start + maxLen));
        ui->tableView->setRowHidden(i, hidden);
    }
}

void UiBucketsTableWidget::on_btnCreateBucket_clicked()
{
    UiCreateBucketDialog dialog(this);
    int ret = dialog.exec();

    // 创建桶
    if (ret == QDialog::Accepted) {
        MyBucket bucket = dialog.getBucket();
        if (bucket.isValid()) {
            QJsonObject params;
            params["bucketName"] = bucket.name;
            params["location"] = bucket.location;
            MG->mGate->send(API::BUCKETS::PUT, params);
        }
    }
}

void UiBucketsTableWidget::onDelBucket()
{
    QModelIndex idx = ui->tableView->currentIndex();

    if (idx.isValid()) {
        QString name = idx.data().toString();
        QMessageBox box(
                QMessageBox::Question, QString::fromLocal8Bit("删除桶"),
                QString::fromLocal8Bit("是否确认删除桶【%1】吗？").arg(name),
                QMessageBox::Yes | QMessageBox::No);

        box.setButtonText(QMessageBox::Yes, QString::fromLocal8Bit("删除"));
        box.setButtonText(QMessageBox::No, QString::fromLocal8Bit("取消"));
        int ret = box.exec();
        if (ret == QMessageBox::Yes) {
            QJsonObject params;
            params["bucketName"] = name;
            MG->mGate->send(API::BUCKETS::DEL, params);
        }
    }
}

void UiBucketsTableWidget::on_btnRefresh_clicked()
{
    MG->mGate->send(API::BUCKETS::LIST);
}
